package net.huashitong.user.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysDeptService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.sys.util.UserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.user.entity.LoginRecord;
import net.huashitong.user.filter.ApkVersionFilter;

import net.huashitong.user.service.LoginRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;

import static net.huashitong.Indicators.utils.DateForIndUtils.getCurDate;

@Controller
@RequestMapping("/app/apkVersion")
public class ApkVersionController extends JsonBaseController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private LoginRecordService loginRecordService;

    private static final String URL = "http://211.149.245.16:8666/downApp/ningde-release.apk";

    @Override
    protected String getView(String arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 版本号控制
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping("/detailApkVersion")
    public Message detailApkVersion(@RequestBody ApkVersionFilter filter) {

        //获取所有单位
        List<SysDept> deptList = sysDeptService.query();
        Map<String, SysDept> deptMap = new HashMap<>();
        for (SysDept sysDept : deptList) {
            if (!deptMap.containsKey(sysDept.getSysId())) {
                deptMap.put(sysDept.getSysId(), sysDept);
            }
        }

        //获取登录信息并做记录
        String userId = filter.getUserId();
        if (StringUtils.isNotBlank(userId)) {
            SysUser user = sysUserService.getById(userId);
            SysLogin login = sysUserService.getSysLoginById(userId);
            if (ObjectUtils.isNotBlank(user) && ObjectUtils.isNotBlank(login)) {
                LoginRecord loginRecord = new LoginRecord();
                loginRecord.setUserId(userId);
                loginRecord.setUserName(user.getName());
                String deptId = login.getDeptId();
                SysDept dept = deptMap.get(deptId);
                loginRecord.setDeptId(deptId);
                if (ObjectUtils.isNotBlank(dept)) {
                    loginRecord.setDeptName(dept.getDeptName());
                } else {
                    loginRecord.setDeptName("未设置部门");
                }
                loginRecord.setLoginTime(getCurDate("yyyyMMdd"));
                loginRecordService.save(loginRecord);
            }
        }

        if ("37".equals(filter.getApkVersion())) {
            return coverMessage("500", "无需更新");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", URL);
        return coverMessage("200", "有新版本请下载", map);
    }
}
